:-module(g64,[]).
:-use_module('de.pro').
:-use_module('gua.pro').
:-use_module('gua8.pro').

new(Var,V):-
	de:new(Var,[count-64,value-V,type-g64,level-6]).


split(Gua,Up,Down):-
	new(Gua,A),
	U is A /\ 7,
	D is A >> 3,
	g8:new(Up,U),
	g8:new(Down,D).

gong(Gua,Gong,Order,Shi):-
	new(Gua,G),
	F = [0,32,48,56,60,62,58,2],
	nth0(Order,F,N),
	E is G xor N,
	A is E /\ 7,
	A is E >> 3,
	g8:new(Gong,A),
	(
	 Order = 0 -> Shi = 5;
	 Order < 6 -> Shi is Order - 1;
	 Shi is 9 - Order
	).

gong(Gua,Dict):-
	gong(Gua,Gong,Order,Shi),
	Dict = _{gong:Gong,order:Order,shi:Shi}.

najia(Gua,Info):-
	gong(Gua,Gong),
	split(Gua,Up,Down),
	g8:najia(Up,Gong.gong,up,R1),
	g8:najia(Down,Gong.gong,down,R2),
	append(R2,R1,R),
	Info = _{gong:Gong,detail:R}.

zhouyi_order(Index,ZyIndex):-
	nth0(ZyIndex,[
		      63,0,34,17,58,23,16,2,59,55,56,7,47,61,8,4,
		      38,25,48,3,37,41,1,32,39,57,33,30,18,45,14,28,
		      15,60,5,40,43,53,10,20,49,35,62,31,6,24,22,26,
		      46,29,36,9,11,52,44,13,27,54,19,50,51,12,42,21
		   ],
	     Index).

de:g64_items([
    '坤'  , '剥'  , '比'  , '观'  , '豫'  , '晋'  , '萃'  , '否',
    '谦'  , '艮'  , '蹇'  , '渐'  , '小过', '旅'  , '咸'  , '遁',
    '师'  , '蒙'  , '坎'  , '涣'  , '解'  , '未济', '困'  , '讼',
    '升'  , '蛊'  , '井'  , '巽'  , '恒'  , '鼎'  , '大过', '姤',
    '复'  , '颐'  , '屯'  , '益'  , '震'  , '噬嗑', '随'  , '无妄',
    '明夷', '贲'  , '既济', '家人', '丰'  , '离'  , '革'  , '同人',
    '临'  , '损'  , '节'  , '中孚', '归妹', '暌'  , '兑'  , '履',
    '泰'  , '大畜', '需'  , '小畜', '大壮', '大有', '夬'  , '乾'
  ]).